<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/25
 * Time: 18:06
 */

namespace Admin\Logic;
use Admin\Transformer\OrderBillTransformer;
use Think\Model;

class OrderBillLogic extends Model
{
    protected $trueTableName = 'wz_order_bill';
    /**
     * @param $params
     * @return mixed
     */
    public function searchOrderBillList($params){
        $result = D('Resource/OrderBill','Service')->searchOrderBillList($params);
        if(is_array($result['dataList']) && count($result['dataList'])){
            foreach ($result['dataList'] as $k=>$bill){
                //admin 信息
                $admin = D('Resource/User','Service')->getUserInfo(['id'=>$bill['admin_id']]);
                $result['dataList'][$k]['department_name'] = $admin['department_name'];
            }
            $tr = new OrderBillTransformer();
            $result['dataList'] = $tr->TransformerCollection($result['dataList']);
        }
        return $result;
    }
    /**
     * @param $params
     * @return mixed
     */
    public function getOrderBillInfo($params){
        $orderBillInfo = D('Resource/OrderBill','Service')->getOrderBillInfo($params);
        return $orderBillInfo;
    }

    /**
     * @param $params
     * @return string
     */
    public function ajaxSaveApplyBill($params){
        $order_id = intval($params['order_id']);
        $bill_id = intval($params['bill_id']);
        $admin_id = intval($params['admin_id']);
        $amount = intval($params['amount']);
        $status = intval($params['status']);
        $userInfo = D('Resource/User','Service')->getUserInfo(['id'=>$admin_id]);

        $params['admin_name'] = $userInfo['username'];
        $params['department_id'] = $userInfo['department_id'];
        $params['arrival_time'] = strtotime($params['arrival_time']);
        if($amount<100000 && $status<1){
            $params['status'] = 10;
            $params['director_review'] = 1;
            $params['g_manager_review'] = 1;
            $params['dp_manager_review'] = 1;
        }
        $orderInfo = D('Resource/Order','Service')->getOrderInfo(['id'=>$order_id]);
        if(!$orderInfo){
            return Response(2001,'未查询到该订单信息');
        }
        $params['order_code'] = $orderInfo['order_code'];
        $params['customer_id'] = $orderInfo['customer_id'];
        if(!$bill_id){
            $result = D('Resource/OrderBill','Service')->addOrderBill($params);
        }else{
            $params['id'] = $bill_id;
            $result = D('Resource/OrderBill','Service')->updateOrderBillInfo($params);
        }

        if(!$result){
            return Response(2002,'请求失败');
        }else{
            return Response(999,'请求成功');
        }
    }

    /**
     * @return string
     */
    public function deleteOrderBill($params){
        $bill_id = I('post.bill_id');
        if(!$bill_id){
            return Response(2001,"请求参数异常");
        }
        $billInfo = D('Resource/OrderBill','Service')->getOrderBillInfo(['id'=>$bill_id]);
        if(!$billInfo){
            return Response(2002,"未查询到该发票信息");
        }
        if($billInfo['status']>0){
            return Response(2003,"该发票正在审核，不可删除");
        }
        $result = D('Resource/OrderBill','Service')->deleteOrderBill(['id'=>$bill_id]);
        if(!$result){
            return Response(2004,'请求失败');
        }else{
            return Response(999,'请求成功');
        }
    }
    /**
     * @param $params
     * @return mixed
     */
    public function searchApprovalOrderBillList($params){
        $uid = $params['uid'];

        $userDepartmentIdList = D('Resource/UserDepartment', 'Service')->getUserRelationDepartmentIdList($uid);

        if($userDepartmentIdList){
            $params['department_id']  = ['in',$userDepartmentIdList];
        }
        unset($params['uid']);
        $result = D('Resource/OrderBill','Service')->searchOrderBillList($params);
        if(is_array($result['dataList']) && count($result['dataList'])){
            foreach ($result['dataList'] as $k=>$bill){
                //admin 信息
                $admin = D('Resource/User','Service')->getUserInfo(['id'=>$bill['admin_id']]);
                $result['dataList'][$k]['department_name'] = $admin['department_name'];
            }
            $tr = new OrderBillTransformer();
            $result['dataList'] = $tr->TransformerCollection($result['dataList']);
        }
        return $result;
    }
}